<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="PLC ID" prop="plcId">
        <el-select v-model="queryParams.plcId" placeholder="请选择" clearable >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="DB块" prop="block">
        <el-input
          v-model="queryParams.block"
          placeholder="请输入DB块"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="具体地址" prop="addr">
        <el-input
          v-model="queryParams.addr"
          placeholder="请输入具体地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input
          v-model="queryParams.note"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="signalList" height="800px"  border>
      <el-table-column label="信号ID" align="center" prop="id" width="100" />
      <el-table-column label="PLC ID" align="center" prop="plcId" width="100" />
      <el-table-column label="DB块" align="center" prop="block" width="100" />
      <el-table-column
        label="具体地址"
        align="center"
        prop="addr"
        width="100"
      />
      <el-table-column
        label="信号的值"
        align="center"
        prop="message"
        width="100"
      />
      <el-table-column label="长度" align="center" prop="length" width="100" />
      <el-table-column label="类型" align="center" prop="type" width="100" />
      <el-table-column label="备注" prop="note" width="400" />
      <el-table-column label="正常最小值" prop="" width="150" />
      <el-table-column label="正常最大值" prop="" width="150" />
      <el-table-column label="超出范围提示说明" prop="" width="200" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['assembly:signal:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改信号详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="信号的值" prop="message">
          <el-input v-model="form.message" placeholder="请输入信号的值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getPlcSignal, editPlcSignal } from "@/api/assembly/signal";
import { getDeviceUse } from "@/api/assembly/device";
export default {
  name: "Signal",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信号详情表格数据
      signalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        plcId: null,
        block: null,
        addr: null,
        message: null,
        length: null,
        type: null,
        direction: null,
        note: null,
        status: null,
        id: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      options: [],
    };
  },
  created() {
    // this.getList();
    this.listDevice()
  },
  methods: {
    // 查询启动设备列表
    listDevice() {
      getDeviceUse().then((res) => {
        console.log('查询启动设备列表',res);
        this.options = res.data;
      });
    },
    /** 查询信号详情列表 */
    getList() {
      this.loading = true;
      getPlcSignal(this.queryParams).then((response) => {
        this.signalList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plcId: null,
        block: null,
        addr: null,
        message: null,
        length: null,
        type: null,
        direction: null,
        note: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改信号详情";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            editPlcSignal(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSignal(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
